/* Copyright (C) 2003, Charles Wang.
 * Author:  Charles Wang <charles@linux.net.cn>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANT; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public LIcense for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#include  <stdio.h>
#include  <stdlib.h>
#include  <string.h>
#include  "newtutils.h"
#include  "mblog.h"

static char ipaddr[20] = "";
static char netmask[20] = "";
static char broadcast[20] = "";

const char * ethercard_runner(void) {
    newtComponent  form, ok_btn, cancel_btn;
    newtGrid       grid, btngrid;
    ipinput        ipaddrii, netmaskii, broadcastii;
    int            v0;
    char           cmdbuf[128], cmd_netmask[48], cmd_broadcast[48];
    const char    *nextstep;

    grid = newtCreateGrid(1, 4);

    ipinput_create(&ipaddrii,    "IP Address", ipaddr);
    ipinput_create(&netmaskii,   "Netmask   ", netmask);
    ipinput_create(&broadcastii, "Broadcast ", broadcast);

    btngrid = newtCreateGrid(2, 1);
    ok_btn = newtButton(-1, -1, "Take Effect");
    newtGridSetField(btngrid, 0, 0, NEWT_GRID_COMPONENT, ok_btn,
		     0, 0, 0, 0, 0, 0);
    cancel_btn = newtButton(-1, -1, "Cancel");
    newtGridSetField(btngrid, 1, 0, NEWT_GRID_COMPONENT, cancel_btn,
		     0, 0, 0, 0, 0, 0);

    newtGridSetField(grid, 0, 0, NEWT_GRID_SUBGRID, ipaddrii.grid,
		     1, 1, 1, 0, 0, 0);
    newtGridSetField(grid, 0, 1, NEWT_GRID_SUBGRID, netmaskii.grid,
		     1, 0, 1, 0, 0, 0);
    newtGridSetField(grid, 0, 2, NEWT_GRID_SUBGRID, broadcastii.grid,
		     1, 0, 1, 0, 0, 0);
    newtGridSetField(grid, 0, 3, NEWT_GRID_SUBGRID, btngrid,
		     1, 1, 1, 0, 0, 0);

    form = newtExtForm(NULL, NULL, 0);
    newtGridAddComponentsToForm(grid, form, 1);

    newtGridWrappedWindow(grid, "Setup ethernet card");
    newtGridFree(grid, 1);

    nextstep = NULL;
    for (;;) {
	if (newtExtRunForm(form) == cancel_btn)  break;

	ipinput_get_ip(&ipaddrii, ipaddr);
	v0 = ipaddrii.v0;
	if (!((v0 >= 1 && v0 <= 126) ||
	      (v0 >= 128 && v0 <= 191) ||
	      (v0 >= 192 && v0 <= 223))) {
	    newtWinMessage("Error", "OK", "The ip address is invalid.");
	    continue;
	}

	ipinput_get_ip(&netmaskii, netmask);
	if (!strcmp(netmask, "INVALID")) cmd_netmask[0] = 0;
	else  sprintf(cmd_netmask, "netmask %s", netmask);

	ipinput_get_ip(&broadcastii, broadcast);
	if (!strcmp(broadcast, "INVALID"))  cmd_broadcast[0] = 0;
	else  sprintf(cmd_broadcast, "broadcast %s", broadcast);

	/* Now we support eth0 only. */
	snprintf(cmdbuf, sizeof(cmdbuf),
		 "/sbin/ifconfig lo 127.0.0.1");
	if (system(cmdbuf)) {
	    mblog("Run '%s' failed.\n", cmdbuf);
	    newtWinMessage("Error", "OK", "ifconfig on lo failed.");
	    continue;
	}
	mblog("Run '%s' successed.\n", cmdbuf);
	snprintf(cmdbuf, sizeof(cmdbuf),
		 "/sbin/ifconfig eth0 %s %s %s",
		 ipaddr, cmd_netmask, cmd_broadcast);
	if (system(cmdbuf)) {
	    mblog("Run '%s' failed.\n", cmdbuf);
	    newtWinMessage("Error", "OK", "ifconfig on eth0 failed.");
	    continue;
	} else {
	    mblog("Run '%s' successed.\n", cmdbuf);
	    newtWinMessage("OK", "OK", "ifconfig on eth0 SUCCESSED.");
	    nextstep = "gateway_dns";
	    break;
	}
    }

    newtFormDestroy(form);
    newtPopWindow();

    return  nextstep;
}

void ethercard_saver(FILE *fp) {
    fprintf(fp, "ipaddr = '%s'\n", ipaddr);
    fprintf(fp, "netmask = '%s'\n", netmask);
    fprintf(fp, "broadcast = '%s'\n", broadcast);
}
